
gradient    = 'axial';

FACE        = 201;
SCENE       = 202;
BODY        = 203;
OBJECT      = 204;
% WORD        = 205;


%% preprocessing of evoked activity

% cut data segement including time window of interest (toi)

toilim          = [-.1 2];
windowlength    = toilim(2) - toilim(1);

cfg             = [];
cfg.toilim      = toilim;

dataSegment     = ft_redefinetrial(cfg, data);

% lpfilter for ERF preprocessing

cfg                 = [];
cfg.channel         = 'MEG';
cfg.dftfilter       = 'no';
cfg.demean          = 'yes';
cfg.baselinewindow  = [-0.1 0];
cfg.detrend         = 'yes';
cfg.lpfreq          = 150;

dataSegment     = ft_preprocessing(cfg, dataSegment);


% timelocked preprocessing

cfg             = [];
cfg.parameter   = 'trial';
cfg.channel     = 'MEG';
cfg.removemean  = 'no';

cfg.keeptrials  = 'yes';
cfg.covariance  = 'no';

cfg.trials      = find(dataSegment.trialinfo == FACE);
dataFaces       = ft_timelockanalysis(cfg, dataSegment);

cfg.trials      = find(dataSegment.trialinfo == SCENE);
dataScenes      = ft_timelockanalysis(cfg, dataSegment);

cfg.trials      = find(dataSegment.trialinfo == BODY);
dataBodies      = ft_timelockanalysis(cfg, dataSegment);

cfg.trials      = find(dataSegment.trialinfo == OBJECT);
dataObjects     = ft_timelockanalysis(cfg, dataSegment);

% cfg.trials      = find(dataSegment.trialinfo == WORD);
% dataWords       = ft_timelockanalysis(cfg, dataSegment);

switch gradient
    
    case 'axial'
        
    case 'planar'
        
        cfg                 = [];
        cfg.method          = 'template';
        neighbours          = ft_prepare_neighbours(cfg, data);
        
        cfg                 = [];
        cfg.planarmethod    = 'sincos';
        cfg.neighbours      = neighbours;
        
        dataPlanarFaces     = ft_megplanar(cfg, dataFaces);
        dataPlanarScenes    = ft_megplanar(cfg, dataScenes);
        dataPlanarBodies    = ft_megplanar(cfg, dataBodies);
        dataPlanarObjects   = ft_megplanar(cfg, dataObjects);
        % dataPlanarWords    = ft_megplanar(cfg, dataWords);
        
        cfg                 = [];
        dataFaces           = ft_combineplanar(cfg, dataPlanarFaces);
        dataScenes          = ft_combineplanar(cfg, dataPlanarScenes);
        dataBodies          = ft_combineplanar(cfg, dataPlanarBodies);
        dataObjects         = ft_combineplanar(cfg, dataPlanarObjects);
        % dataWords          = ft_combineplanar(cfg, dataPlanarWords);
        
    otherwise
        error('Specify axial or planar gradient.')
end

